package com.uduew.sjd.ui.form

import android.content.Intent
import android.os.Bundle
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.databinding.ActivityOcrBinding
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.ui.base.BaseActivity
import com.uduew.sjd.ui.base.StateHolder
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.kongzue.dialogx.dialogs.TipDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.otaliastudios.cameraview.CameraListener
import com.otaliastudios.cameraview.PictureResult
import com.uduew.sjd.ui.permission.PermissionDescription
import com.uduew.sjd.ui.permission.PermissionInterceptor
import com.uduew.sjd.utils.StatusBarUtil
import java.io.File

class OcrActivity : BaseActivity() {


    private lateinit var mStates: OcrActivityStates
    private lateinit var mMessenger: PageMessenger

    override fun initViewModel() {
        mStates = getActivityScopeViewModel(OcrActivityStates::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)
    }


    private lateinit var binding: ActivityOcrBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setStatusBar(this, false, 0, true)
        XXPermissions.with(this)
            .permission(
                Permission.CAMERA
            )
            .interceptor(PermissionInterceptor())
            .description( PermissionDescription())
            .request(object : OnPermissionCallback {
                override fun onGranted(
                    permissions: List<String?>,
                    allGranted: Boolean
                ) {
                    if (!allGranted) {
                        return
                    }
                    binding = (getBinding() as ActivityOcrBinding).apply {
                        camera.setLifecycleOwner(this@OcrActivity)
                        camera.open()
                    }
                }
            })
    }


    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.activity_ocr, BR.vm, mStates).addBindingParam(
            BR.click,
            ClickProxy()
        ).addBindingParam(BR.titleBarListener, TitleBarListener())
            .addBindingParam(BR.cameraListener, CameraHandlerListener())
    }

    class OcrActivityStates : StateHolder() {
        var ocrState: State<String> = State("1")
        var ocrType: State<String> = State("1")
    }

    open inner class ClickProxy {
        fun backPop() {
            finish()
        }

        fun takePic() {
            mStates.ocrState.set("2")
        }
    }

    inner class CameraHandlerListener : CameraListener() {
        override fun onPictureTaken(result: PictureResult) {
            super.onPictureTaken(result)
            val storageDir = filesDir
            val file = File(storageDir, "photo_${System.currentTimeMillis()}.jpg")
            result.toFile(file) { file ->
                if (file != null) {
                    val resultIntent = Intent().apply {
                        putExtra("photoFile", file.absoluteFile.toString())
                    }
                    setResult(RESULT_OK, resultIntent)
                    finish()

                }
            }
        }
    }

    inner class TitleBarListener : OnTitleBarListener {
        override fun onLeftClick(titleBar: TitleBar?) {
            super.onLeftClick(titleBar)
            finish()
        }

        override fun onRightClick(titleBar: TitleBar?) {
            super.onRightClick(titleBar)
        }
    }

}